package canteen.sys.view.individual;

import canteen.sys.model.entity.LoginMaster;
import canteen.sys.model.generic.HibernateDAOFactory;
import canteen.sys.view.ActionView;
import java.util.List;
import javax.swing.JOptionPane;

public class ChangePassword extends javax.swing.JInternalFrame implements ActionView<LoginMaster> {
    LoginMaster Lm = new LoginMaster();
    HibernateDAOFactory hibernateDAOFactory = HibernateDAOFactory.getInstance();
    
    public ChangePassword(LoginMaster lm) {
        Lm = lm;
        initComponents();
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        btnsave = new javax.swing.JButton();
        btncancel = new javax.swing.JButton();
        txtoldpassword = new javax.swing.JPasswordField();
        txtnewpassword = new javax.swing.JPasswordField();
        txtrepassword = new javax.swing.JPasswordField();

        jLabel1.setText("Change password");

        jLabel2.setText("Old password:");

        jLabel3.setText("New password:");

        jLabel4.setText("Re - password:");

        btnsave.setText("Save");
        btnsave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsaveActionPerformed(evt);
            }
        });

        btncancel.setText("Cancel");

        txtnewpassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtnewpasswordActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtoldpassword, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtnewpassword)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(57, 57, 57)
                                        .addComponent(btnsave)
                                        .addGap(18, 18, 18)
                                        .addComponent(btncancel))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(19, 19, 19)
                                        .addComponent(txtrepassword, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(140, 140, 140)
                                .addComponent(jLabel1)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap(56, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtoldpassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtnewpassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtrepassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnsave)
                    .addComponent(btncancel))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtnewpasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtnewpasswordActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtnewpasswordActionPerformed
    
    private void btnsaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsaveActionPerformed
        // TODO add your handling code here:
        if (txtoldpassword.getText().equals(Lm.getlPassword())) {
            if (txtnewpassword.getText().equals(txtrepassword.getText())) {
                Lm.setlPassword(txtnewpassword.getText());
                if (validObject(Lm)) {
                    JOptionPane.showMessageDialog(this, "Change password success!");
                    this.dispose();
                }
            } else {
                JOptionPane.showMessageDialog(this, "Check new password & re new passoword!");
            }
            
        } else {
            JOptionPane.showMessageDialog(this, "Old password not valid!");
        }
    }//GEN-LAST:event_btnsaveActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btncancel;
    private javax.swing.JButton btnsave;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPasswordField txtnewpassword;
    private javax.swing.JPasswordField txtoldpassword;
    private javax.swing.JPasswordField txtrepassword;
    // End of variables declaration//GEN-END:variables

    @Override
    public void deleteRow(int index) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public void updateRow(LoginMaster row, int index) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public void addRow(LoginMaster row, boolean posion) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public void showList(List<LoginMaster> list) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public void showDetail(LoginMaster object) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public LoginMaster getObject(int index) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public void setObject(LoginMaster object) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public boolean validObject(LoginMaster Lm) {
        boolean check = false;
        LoginMaster itemMasterResult = hibernateDAOFactory.getLoginDAO().makePersistent(Lm);
        if (itemMasterResult.getlPassword().equals(txtnewpassword.getText())) {
            check = true;
        }
        return check;
    }
}
